﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using MockPrjTeam4.Biz;
using System.Data;
 

namespace MockPrjTeam4.Pages.Forms
{
    public partial class PopupAddress : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            gvAddress.DataSource = AddressBiz.Search("", "", "");
            gvAddress.DataBind();
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            txtPostcode.Text = "";
            txtStreet.Text = "";
            txtTown.Text = "";
        }

        protected void btnNone_Click(object sender, EventArgs e)
        {
            StringBuilder script = new StringBuilder();
            script.Append("<script type='text/javascript'>");
            script.AppendFormat("window.opener.document.getElementById('txtAddLine1').value = '{0}';", "");
            script.AppendFormat("window.opener.document.getElementById('txtAddLine2').value = '{0}';", "");
            script.AppendFormat("window.opener.document.getElementById('txtAddLine3').value = '{0}';", "");
            script.Append("this.close();");
            script.Append("</script>");
            ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "key01", script.ToString(), false);
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            gvAddress.DataSource = AddressBiz.Search(txtPostcode.Text, txtStreet.Text, txtTown.Text);
            gvAddress.DataBind();
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            StringBuilder script = new StringBuilder();
            script.Append("<script type='text/javascript'>");
            script.Append("this.close();");
            script.Append("</script>");
            ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "key01", script.ToString(), false);
        }

        protected void btnSelect_Click(object sender, EventArgs e)
        {
            int id = (int)gvAddress.SelectedDataKey.Values["AddressId"];
            DataTable dt = AddressBiz.SearchById(id);
            DataRow row = dt.Rows[0];
            StringBuilder script = new StringBuilder();
            script.Append("<script type='text/javascript'>");
            script.AppendFormat("window.opener.document.getElementById('hdfAddressId').value = '{0}';", row[0]);
            script.AppendFormat("window.opener.document.getElementById('txtAddLine1').value = '{0}';", row[1]);
            script.AppendFormat("window.opener.document.getElementById('hdfAddLine1').value = '{0}';", row[1]);
            script.AppendFormat("window.opener.document.getElementById('txtAddLine2').value = '{0}';", row[2]);
            script.AppendFormat("window.opener.document.getElementById('hdfAddLine2').value = '{0}';", row[2]);
            script.AppendFormat("window.opener.document.getElementById('txtAddLine3').value = '{0}';", row[3]);
            script.AppendFormat("window.opener.document.getElementById('hdfAddLine3').value = '{0}';", row[3]);
            script.AppendFormat("window.opener.document.getElementById('txtPostcode').value = '{0}';", row[4]);
            script.AppendFormat("window.opener.document.getElementById('hdfPostcode').value = '{0}';", row[4]);
            script.AppendFormat("window.opener.document.getElementById('txtTown').value = '{0}';", row[6]);
            script.AppendFormat("window.opener.document.getElementById('hdfTown').value = '{0}';", row[6]);
            script.AppendFormat("window.opener.document.getElementById('hdfTownId').value = '{0}';", row[5]);
            script.AppendFormat("window.opener.document.getElementById('txtCounty').value = '{0}';", row[8]);
            script.AppendFormat("window.opener.document.getElementById('hdfCounty').value = '{0}';", row[8]);
            script.AppendFormat("window.opener.document.getElementById('hdfCountyId').value = '{0}';", row[7]);
            script.AppendFormat("window.opener.document.getElementById('txtCountry').value = '{0}';", row[10]);
            script.AppendFormat("window.opener.document.getElementById('hdfCountry').value = '{0}';", row[10]);
            script.AppendFormat("window.opener.document.getElementById('hdfCountryId').value = '{0}';", row[9]);
            script.Append("this.close();");
            script.Append("</script>");
            ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "key01", script.ToString(), false);
        }

        protected void gvAddress_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvAddress.PageIndex = e.NewPageIndex;
        }
    }
}